
const state = () => ({
    JWT_TOKEN: window.sessionStorage.getItem('token'),
})

// getters
const getters = {
    getToken (state) {
        if (!state.JWT_TOKEN) {
            state.JWT_TOKEN = window.sessionStorage.getItem('token');
        }
        return state.JWT_TOKEN;
    }
}

// actions
const actions = {
    userLogin({ commit }, token) {
        commit('saveToken', token);
    },
    userLogout({ commit }) {
        commit('removeToken');
    }
}

// 不要忘记第一个参数 state
const mutations = {
    saveToken(state, token) {
        state.JWT_TOKEN = token;
        window.sessionStorage.setItem('token', JSON.stringify(token));
    },

    removeToken(state) {
        window.sessionStorage.setItem('token', '');
        state.JWT_TOKEN = '';
    }
}


export default {
    namespaced: true,
    state,
    getters,
    actions,
    mutations
}
